import {loginAndRegister, getLoginSendcode} from '../../../utils/api/module/login';

Page({
    data: {
        // 本页所有的图片
        imgs: {
            bgImg: "https://ythapi.bjyangshu.com/public/images/login-bg-img.png",
        },
        phoneVal: null,
        codeVal:null,
        passwordVal:null,
        codeText:"获取验证码",
        disabled: true ,
        currentTime: 60,
        autoFocusCode: false
    },
    onLoad: function (options) {

    },
    /**
     * 输入手机号双向绑定
     */
    getPhoneVal(e){
        this.setData({phoneVal: e.detail.value, autoFocusCode: false});
    },
    /**
     * 输入验证码双向绑定
     */
    getCodeVal(e){
        this.setData({codeVal:e.detail.value});
    },
    /**
     * 输入新密码双向绑定
     */
    getPasswordVal(e){
        this.setData({passwordVal: e.detail.value, autoFocusCode: false});
    },
    /**
     * 获取验证码
     */
    getCodeApi(){
        let phone = this.data.phoneVal;
        if(!this.data.disabled){
            return false;
        }else if(phone == null){
            wx.showToast({
                title: "请输入电话号",
                icon: "error"
            });
            return false;
        }else if(!(/^1[345678]\d{9}$/.test(phone))){
           console.log();
            wx.showToast({
                title: "电话号码格式错误",
                icon: "error"
            });
            return false;
        }else{
            /*api获取验证码*/
            getLoginSendcode({
                type: 1,
                mobile: phone
            }).then(res => {
                if(res.data.code != 1) {
                    wx.showToast({title: res.data.message, icon: 'none'});
                    return false;
                };
                if(res.data.code == 1) {
                    this.setData({ disabled: false, autoFocusCode: true });
                    this.times();
                    this.data.codeData = res.data.message;
                };
            });
        }
    },
    // 倒计时方法
    times: function(options) {
        let that = this;
        let currentTime = that.data.currentTime
        this.data.interval = setInterval(() => {
            currentTime--;
            that.setData({
                codeText: currentTime + '秒后重试'
            })
            if (currentTime <= 0) {
                clearInterval(this.data.interval) //清除定时器
                that.setData({ //对定时器重新设置
                    codeText: '重新发送',
                    currentTime: 60,
                    disabled: true
                })
            }
        }, 1000)
    },
    /**
     * 确认更新密码
     */
    confirmPassword(){
        let phoneVal= this.data.phoneVal;
        let codeVal= this.data.codeVal;
        let passwordVal= this.data.passwordVal;
        if(phoneVal == null){
            wx.showToast({
                title: "请输入电话号",
                icon: "error"
            });
            return false;
        }else if(!(/^1[345678]\d{9}$/.test(phoneVal))){
           console.log();
            wx.showToast({
                title: "电话号码格式错误",
                icon: "error"
            });
            return false;
        }else if(!this.data.codeVal){
            wx.showToast({
                title: "验证码不能为空",
                icon: "error"
            });
            return false;
        } else if(Number(this.data.codeData) !== Number(codeVal)) {
            wx.showToast({
                title: "验证码不正确",
                icon: "error"
            });
            return false;
        };
        if(!passwordVal) {
            wx.showToast({
                title: "密码不能为空",
                icon: "error"
            });
        }
        loginAndRegister({
            type: 2,
            mobile: phoneVal,
            password: passwordVal,
            messagecode: codeVal,
        }).then(res => {
            wx.showToast({title: res.data.message, icon: "none", duration: 2200});
            if(res.data.code == 1) {
                setTimeout(() => {
                    wx.reLaunch({
                        url: '/pages/loginAndRegister/login/index'
                      });
                }, 1000)
            };
        })
    },
    onHide: function() {
        clearInterval(this.data.interval);
    },
    onUnload: function() {
        clearInterval(this.data.interval);
    }
});